Page({
  courseAll(){
    wx.navigateTo({
      url: '../../../pages/zixun/courseAll/courseAll',
    })
  },
  replyAll(){
   wx.navigateTo({
     url: '../../../pages/zixun/replyAll/replyAll',
   })
  },
  huafeiAll(){
    wx.navigateTo({
      url: '../../../pages/zixun/huafeiAll/huafeiAll',
    })
   },
   zixunnew(){
     let tt=this
     wx.navigateTo({
       url: '../../../pages/zixun/zixunnew/zixunnew',
       success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: tt.data.detail})
      }
     })
   },
  data: {
    zxUrl: "http://localhost:8080/file/downLoad?fileName=",
    detail: {},
    chargeList: [], // 收费列表
    replyList: [], // 回复列表
    pingjiaList: [], // 评价列表
    courseList: [], // 课程列表
    showLoading: true ,
    detail:{}
  
  },

  async onLoad(option) {
    const eventChannel = this.getOpenerEventChannel();
      let t=this
    // 1. 接收咨询师详情
    const detail = await new Promise((resolve) => {
      eventChannel.on('acceptDataFromOpenerPage', (data) => {
        t.setData({
          detail:data.data
        })
        resolve(data.data);
      });
    });
    this.setData({ detail, showLoading: false });
    wx.setNavigationBarTitle({ title: detail.name });

    // 2. 并行调用四个接口等待detail加载完成
    try {
      const [chargeRes, replyRes, pingjiaRes, courseRes] = await Promise.all([
        this.fetchChargeList(detail.id),
        this.fetchReplyList(detail.id),
        this.fetchPingjiaList(detail.id),
        this.fetchCourseList(detail.id)
      ]);

      this.setData({
        chargeList: chargeRes.list,
        replyList: replyRes.list,
        pingjiaList: pingjiaRes.list,
        courseList: courseRes.list
      });

    } catch (error) {
      console.error('接口调用失败:', error);
      wx.showToast({
        title: '传值错误',
        icon: 'none',
        duration: 2000
      });
    } finally {
      this.setData({ showLoading: false });
    }
  },

  // 1. 收费接口
  fetchChargeList(consultId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `http://localhost:8080/charge/all`,
        data: { consultId, pageNum: 1, pageSize: 4 },
        success: (res) => {
          if (res.statusCode === 200 && res.data.code === 1) {
            resolve({
              list: res.data.data.list.map(item => ({
                type: item.chargeType.name,
                price: item.chargePrice.toFixed(2),
                count: item.countNumber,
                unit: "/次(50分钟)"
              }))
            });
          } else reject('收费接口错误');
        },
        fail: () => reject('收费网络错误')
      });
    });
  },

  // 2. 回复接口
  fetchReplyList(consultId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:8080/questionReply/selectAll',
        data: { consult_id: consultId },
        success: (res) => {
          if (res.data.code === 1) {
            resolve({
              list: res.data.data
                .filter(item => item.consult_id === String(consultId))
                .map(item => ({
                  content: item.content,
                  reply: item.reply,
                  createTime: item.create_time?.slice(0, 10) || '未知时间'
                }))
            });
          } else reject('回复接口错误');
        },
        fail: reject
      });
    });
  },

  // 3. 用户评价接口
  fetchPingjiaList(consultId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:8080/userMark/findAll',
        data: { 
          pageNum: 1, 
          pageSize: 4, 
          consultId,
          mark_text: ''  
        },
        success: (res) => {
          if (res.data.code === 1) {
            // console.log(res.data.data.list)
            resolve({
              list: res.data.data.list.map(item => ({
                content: item.mark_text,
                star: item.star,
                starArray: Array.from({ length: item.star }),
                createTime: item.create_time?.slice(0, 10) || '未知时间'
              }))
            });
          } else reject('评价接口错误');
        },
        fail: reject
      });
    });
  },

  // 4. 课程接口
  fetchCourseList() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:8080/course/selectAll?pageNum=1&pageSize=5&id=&courseName=&tabularChart=&courseTime=&courseMount=&courseIntroduce=&applicablePopulation=&typeId=&learnHarvest=&subscriptionNotice=&collects=&teacherId=&courseVideo=&originalPrice=&discountPrice=&validityPeriod=',
        data: { 
          pageNum: 1, 
          pageSize: 4,
          courseName:'',
          tabularChart:''
        },
        success: (res) => {
          if (res.data.code === 1) {
            // console.log(res.data.data.list)
            resolve({
              list: res.data.data.list.map(item => ({
               name1:item.courseName,
               videe:item.tabularChart
              }))
            });
          } else reject('课程接口错误');
        },
        fail: () => reject('课程网络错误')
      });
    });
  },
});